// ----------------------------------------------------------------------------//
// ExceptionLogInfo.java
// Created by user On 2012-10-23
package com.demo.framework.log;



/**
 * 
* @ClassName: ExceptionLogInfo 
* @Description: TODO(记录错误记录，存在数据库表中) 
* @author 谢云腾
* @date 2013-3-22 下午04:32:12 
* @version 1.0
 */
public class ExceptionLogInfo {
//	private final static Logger LOG = LoggerFactory.getLogger(ExceptionLogInfo.class);

	public String description = "UNKNOWN";
	public String className = "UNKNOWN";
	public String message = "UNKNOWN";
	public String traceClassName = "UNKNOWN";
	public String traceMethodName = "UNKNOWN";
	public int traceLineNumber = 0;
	public String causeClassName = "UNKNOWN";
	public String causeMessage = "UNKNOWN";
	public String causeTraceClassName = "UNKNOWN";
	public String causeTraceMethodName = "UNKNOWN";
	public int causeTraceLineNumber = 0;
	public String timestamp;

	public static String firstLogMessage = "";
	public static String secondLogMessage = "";

	public ExceptionLogInfo(String description, Throwable ex, String timestamp, String packagePrefix) {
		this.timestamp = timestamp;
		this.description = description;
		if (ex != null) {
			className = ex.getClass().getName();
			message = ex.getMessage() == null ? "null" : ex.getMessage();
			StackTraceElement e = getFirstMatchedTraceElement(ex, packagePrefix);
			if (e != null) {
				traceClassName = e.getClassName();
				traceMethodName = e.getMethodName();
				traceLineNumber = e.getLineNumber();
			}
			Throwable c = ex.getCause();
			if (c != null) {
				causeClassName = c.getClass().getName();
				causeMessage = c.getMessage() == null ? "null" : c.getMessage();
				e = getFirstMatchedTraceElement(c, packagePrefix);
				if (e != null) {
					causeTraceClassName = e.getClassName();
					causeTraceMethodName = e.getMethodName();
					causeTraceLineNumber = e.getLineNumber();
				}
			}
		}

		String logMessage = " className=" + className + ", message=" + message + ", traceClassName=" + traceClassName + ", traceMethodName=" + traceMethodName + ", traceLineNumber=" + traceLineNumber;
		if (logMessage.equals(firstLogMessage) || logMessage.equals(secondLogMessage)) {
			secondLogMessage = firstLogMessage;
			firstLogMessage = logMessage;
			return;
		} else {
			secondLogMessage = firstLogMessage;
			firstLogMessage = logMessage;
		}

		//save data

	}

	public StackTraceElement getFirstMatchedTraceElement(Throwable ex, String packagePrefix) {
		StackTraceElement elements[] = ex.getStackTrace();
		if (elements.length == 0)
			return null;
		if (packagePrefix != null) {
			for (int i = 0; i < elements.length; ++i) {
				if (elements[i].getClassName().startsWith(packagePrefix)) {
					return elements[i];
				}
			}
		}
		return elements[0];
	}

	@Override
	public String toString() {
		return "ExceptionLogInfo [imestamp = " + timestamp + " description = " + description + " className=" + className + ", message=" + message + ", traceClassName=" + traceClassName
				+ ", traceMethodName=" + traceMethodName + ", traceLineNumber=" + traceLineNumber + ", causeClassName=" + causeClassName + ", causeMessage=" + causeMessage + ", causeTraceClassName="
				+ causeTraceClassName + ", causeTraceMethodName=" + causeTraceMethodName + ", causeTraceLineNumber=" + causeTraceLineNumber + "]";
	}
}

// ----------------------------------END---------------------------------------//